<template>
  <div class="chart-container">
    <div id="water-supply-flow-chart" :style="getAvailableHeight"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import moment from "moment";

require('echarts/theme/macarons') // echarts theme

export default {
  name: 'WaterSupplyMonitor',
  data() {
    return {
      screenHeight: null,
      mode: 'chart',
      waterSupplyFlowChart: undefined,
      waterSupplyPressChart: undefined,
      baseOption: {
        legend: {
          data: ['进水流量 m³/h', '出水流量 m³/h', '压力 MPa']
        },
        grid: {
          top: '5%',
          right: '5%',
          bottom: 65,
          left: '6%',
          containsLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        dataZoom: [{
          "show": true,
          "height": 30,
          "xAxisIndex": [
            0
          ],
          bottom: 5,
          "start": 10,
          "end": 80,
          handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
          handleSize: '110%',
          handleStyle: {
            color: "#d3dee5",

          },
          textStyle: {
            color: "#fff"
          },
          borderColor: "#90979c"
        }, {
          "type": "inside",
          "show": true,
          "height": 15,
          "start": 1,
          "end": 35
        }],
        xAxis: {
          type: 'category',
          data: [],
        },
        yAxis: [
          {
            type: 'value',
            axisLine: {
              show: true,
            },
            axisLabel: {
              formatter: '{value} m³/h'
            }
          },
          {
            type: 'value',
            position: 'right',
            axisLine: {
              show: true,
            },
            axisLabel: {
              formatter: '{value} MPa'
            }
          }
        ],
        series: [
          {
            name: '进水流量 m³/h',
            data: [],
            type: 'line',
            smooth: true
          },
          {
            name: '出水流量 m³/h',
            data: [],
            type: 'line',
            smooth: true
          },
          {
            name: '压力 MPa',
            data: [],
            type: 'line',
            smooth: true,
            yAxisIndex: 1
          }
        ]
      },
    }
  },
  methods: {
    handleModeClick(mode) {
      this.mode = mode
    },
    initChart() {
      this.waterSupplyFlowChart = echarts.init(document.getElementById('water-supply-flow-chart'), 'macarons')
    },
    resizeCharts(height) {
      this.screenHeight = height
      this.$nextTick(() => {
        this.waterSupplyFlowChart.resize()
      })
    },
    updateOption(data) {
      const xAxisData = data.timeList.map(item => {
        const m = new moment(item)
        return m.format('MM-DD HH:mm')
      })
      const option = Object.assign({}, this.baseOption)
      option.xAxis.data = xAxisData
      option.series[0].data = data.inlet
      option.series[1].data = data.outlet
      option.series[2].data = data.pressList
      this.waterSupplyFlowChart.setOption(option)
    }
  },
  computed: {
    getAvailableHeight() {
      const h = this.screenHeight - 528
      return {width: '100%', height: h + 'px'}
    }
  },
  created() {
    this.screenHeight = document.body.clientHeight
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  }
}

</script>

<style scoped lang="scss">
.chart-container {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
